<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Default_Model_News extends ZF_DB{
    protected $_table_name = "news" ;
    
    protected $_id;
    protected $_title;
    protected $_title_en;
    protected $_content;
    protected $_content_en;
    protected $_des;
    protected $_des_en;
    protected $_thumbnail_url;
    protected $_type;
    protected $_view_counter;
    protected $_timestamp;
    protected $_active;
    
    public function getTitle(){
        return $this->_title;
    }
    public function setTitle($title){
        $this->_title = $title;
    }
    public function getContent(){
        return $this->_content;
    }
    public function setContent($content){
        $this->_content = $content;
    }
    public function getDes(){
        return $this->_des;
        
    }
    public function setDes($des){
        $this->_des = $des;
    }
    public function getTitle_En(){
        return $this->_title_en;
    }
    public function setTitle_En($title_en){
        $this->_title_en = $title_en;
    }
    public function getContent_En(){
        return $this->_content_en;
    }
    public function setContent_En($content_en){
        $this->_content_en = $content_en;
    }
    public function getDes_En(){
        return $this->_des_en;
        
    }
    public function setDes_En($des_en){
        $this->_des_en = $des_en;
    }
    public function getThumbnailUrl(){
        return $this->_thumbnail_url;
    }
    public function setThumbnailUrl($thumbnail_url){
        $this->_thumbnail_url = $thumbnail_url;
    }
    public function getType(){
        return $this->_type;
    }
    public function setType($type){
        $this->_type = $type;
    }
    public function getView_Counter(){
        return $this->_view_counter;
    }
    public function setView_Counter($view_counter){
        $this->_view_counter = $view_counter;
    }
    public function getTimestamp(){
        return $this->_timestamp;
    }
    public function setTimestamp($timestamp){
        $this->_timestamp = $timestamp;
    }

  
    public function getId(){
        return $this->_id;
    }
    public  function setId($id){
        $this->_id = $id;
    }
   
    public function getActive(){
        return $this->_active;
    }
    public function setActive($active){
        $this->_active = $active;
    }
 
    
    public function insert($title,
            $thumbnail_url,$timestamp,      
            $title_en = "",
            $content = "",$content_en ="" ,
            $des = "",$des_en ="" ,
            $active = 1,$view_counter = 0,$type = 0){
        
        if($title && $timestamp && $thumbnail_url){             
                $data =  array(
                    'title' =>$title,
                    'title_en'=>$title_en,
                    'content' =>$content,
                    'content_en'=>$content_en,
                    'des'=>$des,
                    'des_en'=>$des_en,
                    'type'=>$type,
                    'timestamp'=>$timestamp,
                    'thumbnail_url'=>$thumbnail_url,
                    'view_counter' =>$view_counter,
                    'active'=>$active);
            return         $this->db->insert($this->_table_name,$data);
	
	}
        return -1;
	
    }
   
    public function update(){
        $where  = "id = $this->_id";
	$data =  array(
                    'title' =>$this->_title,
                    'title_en'=>$this->_title_en,
                    'content' =>$this->_content,
                    'content_en'=>$this->_content_en,
                    'des'=>$this->_des,
                    'des_en'=>$this->_des_en,
                    'type'=>$this->_type,
                    'timestamp'=>$this->_timestamp,
                    'thumbnail_url'=>$this->_thumbnail_url,
                    'view_counter' =>$this->_view_counter,
                    'active'=>$this->_active);
        
	return $this->db->update(
                $this->_table_name,
                $data,$where);
    }
    public function getById($id){
        if($id)
	{
		$select = $this->db->select()
			->from(array("p"=>$this->_table_name))
                        ->where("p.id = ?",$id );
		
		$data = $this->db->fetchRow($select);
		if($data){
			$this->_id = $data['id'];
			$this->_title = $data['title'];
			$this->_title_en = $data['title_en'];
			$this->_content = $data['content'];
                        $this->_content_en = $data['content_en'];       
                        $this->_view_counter = $data['view_counter'];
                        $this->_des = $data['des'];
                        $this->_des_en = $data['des_en'];
                        $this->_type = $data['type'];
                        $this->_timestamp = $data['timestamp'];
                        $this->_thumbnail_url = $data['thumbnail_url'];
                        $this->_active = $data['active'];
			
		}
                return $data;
	}
	return -1;
    }
   
    public function listAll($limit = 0,$orderby = "id"){
        $select = $this->db->select()
                ->from(array("p" => $this->_table_name))
                
                ->where("active = ?",1)
                ->order("p.".$orderby." DESC")
                ->limit($limit,0);
	$data = $this->db->fetchAll($select);
	return $data;
    }
    public function delete(){
            $this->getById($this->_id);
            $this->_active = 0;
            return $this->update();
    }
}
?>
